<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Banner extends Base
{
    /**
     * @param Request $request
     *
     */
    public function index(Request $request)
    {
        $page    = $request->param('page') ?: 1;
        $limit   = $request->param('limit') ?: 15;
        $keyword = $request->param('keyword') ?: '';
        $data    = Db::table('ad_banner')->where(['deleted_at' => 0])->paginate($limit, false,
            [
                'page' => $page,
                'query' => [
                    'keyword' => $keyword
                ]
            ]);
        return view('index', compact('data'));
    }

    /**
     * @param Request $request
     */
    public function save(Request $request)
    {
        $sort   = $request->param('sort');
        $status = $request->param('status');
        $image  = $request->param('img_url');
        $url    = $request->param('url');
        if (Db::table('ad_banner')->insertGetId([
            'img' => $image,
            'url' => $url,
            'status' => $status,
            'sort' => $sort,
            'create_time' => time()
        ])
        ) {
            $this->success('success');
            $this->redirect('admin/banner/index');
        }
        $this->error('添加失败');
    }


    public function updateOnlineType(Request $request)
    {
        $status = $request->param('status');
        $id     = $request->param('id');

        $result = Db::table('ad_banner')->where('id', $id)->update(['status' => $status]);
        if ($result) {
            return json(['status' => 1]);
        }
        return json(['status' => 0]);

    }

    /**
     * @return \think\response\View
     */
    public function add()
    {
        return view('add');
    }

    /**
     * @param Request $request
     */
    public function edit(Request $request)
    {
        $data = Db::table('ad_banner')->find($request->param('id'));
        return view('edit', compact('data'));
    }


    /**
     * @param Request $request
     */
    public function update(Request $request)
    {
        $sort   = $request->param('sort');
        $status = $request->param('status');
        $image  = $request->param('img_url');
        $url    = $request->param('url');
        $id     = $request->param('id');
        if (Db::table('ad_banner')->where(['id' => $id])->update([
            'img' => $image,
            'url' => $url,
            'status' => $status,
            'sort' => $sort,
        ])
        ) {
            $this->success('编辑成功');
        }
        $this->error('编辑失败');
    }

    /**
     * @param Request $request
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');

        //删除多条
        if (empty($id)) {
            $ids = $_REQUEST['ids'];
            foreach ($ids as $k => $v) {
                Db::table('ad_banner')->where(['id' => $v])->update(['deleted_at' => 1]);
            }
            $this->success('删除成功');
        } else {//单条
            if (Db::table('ad_banner')->where(['id' => $id])->update(['deleted_at' => 1])) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
    }


    /**
     * @param Request $request
     * @return \think\response\Json
     * 图片跳转链接
     */
    public function img_link(Request $request)
    {
        $name  = $request->param('name');
        $image = $request->param('image');//图片上传的时候调用通用上传类，将返回的图片路径传过来
        $url   = $request->param('url');

        if ($name and $image and $url) {
            Db::table('ad_banner')->insertGetId([
                'name' => $name,
                'img' => $image,
                'url' => $url,
                'create_time' => time()
            ]);
        }
        $data = Db::table('ad_banner')->select();
        return json(['code' => 200, 'info' => 'success', 'data' => $data]);
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     * 图片修改
     */
    public function img_update(Request $request)
    {
        $id     = $request->param('id');
        $url    = $request->param('url');
        $is_top = $request->param('is_top');
        $data   = Db::table('ad_banner')->where(['id' => $id])->update(['url' => $url, 'is_top' => $is_top]);
        return json(['code' => 200, 'info' => 'success', 'data' => $data]);

    }

}